package com.shirley.customcontrols;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.shirley.android.breadcrumb.BreadCrumb;
import com.shirley.android.breadcrumb.BreadCrumbBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 面包屑控件fragment
 * Created by ZLJ on 2017/12/18.
 */
public class BreadCrumbFragment extends Fragment {

    Unbinder unbinder;
    @BindView(R.id.bread_crumb)
    BreadCrumb breadCrumb;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bread_crumb, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        breadCrumb.setData(initBreadCrumbData());
    }

    public List<BreadCrumbBean> initBreadCrumbData(){

        List<BreadCrumbBean> data = new ArrayList<>();

        BreadCrumbBean firstBean = new BreadCrumbBean();
        firstBean.setText("第1层");
        firstBean.setLayer(0);
        List<BreadCrumbBean> children = initBreadCrumbBean(1);
        firstBean.setChildren(children);
        data.add(firstBean);

        return data;
    }

    public List<BreadCrumbBean> initBreadCrumbBean(int layer){
        List<BreadCrumbBean> children = new ArrayList<>();
        for (int i = 0; i < 5; i++){
            BreadCrumbBean bean = new BreadCrumbBean();
            bean.setText("第" + (i + 1) + "子元素(第" + layer + "层)");
            bean.setLayer(layer);
            if (layer < 5){
                int nextLayer = layer + 1;
                List<BreadCrumbBean> grandChildren = initBreadCrumbBean(nextLayer);
                bean.setChildren(grandChildren);
            }
            children.add(bean);
        }
        return children;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
